package com.mxfcode.back.controller;

import com.mxfcode.back.service.BackAnswerQuestionsService;
import com.mxfcode.bbs_common.response.PageDataResult;
import com.mxfcode.bbs_common.util.AOSUtils;
import com.mxfcode.bbs_common.util.community.CommunityConstant;
import com.mxfcode.entity.BBSAnswerQuestions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author moxiufeng
 * @date 2021/2/27 23:03
 */
@Controller
@RequestMapping("/back/answerQuestions")
public class BackAnswerQuestionsController implements CommunityConstant {
    private static final Logger logger = LoggerFactory.getLogger(BackAnswerQuestionsController.class);
    @Autowired
    private BackAnswerQuestionsService answerQuestionsService;

    @PostMapping("answerQuestionsList")
    @ResponseBody
    public PageDataResult articleList(@RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize){
        logger.info("获取问答列表");
        PageDataResult pdr = new PageDataResult();
        try {
            if(null == pageNum) {
                pageNum = 1;
            }
            if(null == pageSize) {
                pageSize = 10;
            }
            // 获取服务类目列表
            pdr = answerQuestionsService.getAnswerQuestionsList(pageNum ,pageSize);
            logger.info("问答列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("问答列表查询异常！", e);
        }
        return pdr;
    }

    @PostMapping("/setAnswerQuestions")
    @ResponseBody
    public Map<String,Object> setAnswerQuestions(BBSAnswerQuestions answerQuestions){
        logger.info("用户设置问答[新增或更新]！");
        Map<String,Object> data = new HashMap();
        if(AOSUtils.isEmpty(answerQuestions.getId())){
            //新增
            data = answerQuestionsService.addAnswerQuestions(answerQuestions);
        }else{
            //修改
            data = answerQuestionsService.updateAnswerQuestions(answerQuestions);
        }
        return data;
    }

    @PostMapping("del")
    @ResponseBody
    public Map<String, Object> del(@RequestParam("id") String id) {
        logger.info("删除问答！id:" + id);
        Map<String, Object> data = answerQuestionsService.del(id);
        return data;
    }
}
